<?php
// ini_set('memory_limit', '-1');
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
require_once 'webservice.php';
class Webjson extends MY_Controller {
	//private $ws_url = 'http://127.0.0.1:2048/webservice/webserver';
	private $ws_url = 'http://opper.com/webservice/webserver';
	public function __construct() {
		parent::__construct ();
		$this->ws_url =  base_url().'webservice/webserver';
	}
	
	public function index() {
		$params = array ();
		if ($this->input->post ( 'submit' )) {
			$result = '';
			$params ['clientId'] = $this->input->post ( 'clientId' );
			$params ['password'] = $this->input->post ( 'password' );
			$params = json_encode ( $params );
			$function = $this->input->post ( 'function' );
			
			$this->load->library ( 'zend' );
			
			$this->zend->load ( 'Zend_Rest_Client' );
			
			$client = new Zend_Rest_Client ( $this->ws_url );
			//print_r($client);
			//echo $function;exit;
			$http = $client->getHttpClient ();
			$http->setConfig ( array (
					'timeout' => 300 
			) );
			
			switch ($function) {
				case 'checkLogin' :
					$result = $client->checkLogin ( $params )->post ();
					break;
				case 'getCategorys' :
					$result = $client->getCategorys ( $params )->post ();
					break;
				case 'getListProvider' :
					$result = $client->getListProvider ( $params )->post ();
					break;
				case 'getListProduct' :
					$result = $client->getListProduct ( $params )->post ();
					break;
				case 'registerProfile' :
					$result = $client->registerProfile ( $params )->post ();
					break;
				case 'addMember' :
					$result = $client->addMember ( $params )->post ();
					break;
				case 'updateProfile' :
					$result = $client->updateProfile ( $params )->post ();
					break;
				case 'updateMember' :
					$result = $client->updateMember ( $params )->post ();
					break;
				case 'addContract' :
					$result = $client->addContract ( $params )->post ();
					break;
				case 'getListContract' :
					$result = $client->getListContract ( $params )->post ();
					break;
				case 'updateContract' :
					$result = $client->updateContract ( $params )->post ();
					break;
				case 'getSomeInfo' :
					$result = $client->getSomeInfo ( $params )->post ();
					break;
				case 'addTerminalLetter' :
					$result = $client->addTerminalLetter ( $params )->post ();
					break;
				case 'getListContractHistory' :
					$result = $client->getListContractHistory ( $params )->post ();
					break;
				default :
					// code...
					break;
			}
			$params = json_decode ( $params, true );
			$params ['client_id'] = $this->input->post ( 'clientId' );
			$params ['result'] = $result->response ();
		}
		
		$params ['functions'] = array (
				'checkLogin' => 'checkLogin',
				'getCategorys' => 'getCategorys',
				'getListProduct' => 'getListProduct',
				'getListProvider' => 'getListProvider',
				'registerProfile' => 'registerProfile',
				'addMember' => 'addMember',
				'updateProfile' => 'updateProfile',
				'updateMember' => 'updateMember',
				'addContract' => 'addContract',
				'getListContract' => 'getListContract',
				'updateContract' => 'updateContract',
				'getSomeInfo' => 'getSomeInfo',
				'addTerminalLetter' => 'addTerminalLetter',
				'getListContractHistory' => 'getListContractHistory' 
		)
		;
		$params ['func'] = $this->input->post ( 'function' );
		$this->load->view ( 'test.php', $params );
	}
	
	function checkLogin($username,$password){
		$request['clientId'] = $username;
		$request['password'] = $password;
		$server = new Webservice();
		print_r($request);
		$return = $server->checkLogin($request);
		print_r($return);
		exit;
	}
}
